package com.lg.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.lg.dao.Criteria;
import com.lg.dao.Example;
import com.lg.dao.criteria.AngelCandidateCriteria;
import com.lg.dao.criteria.AngelPicturesCriteria;
import com.lg.model.AngelCandidate;
import com.lg.model.AngelPictures;
import com.lg.model.enums.CommonEnum.Del_Flag;
import com.lg.model.exception.BusinessException;
import com.lg.model.exception.OtherException;
import com.lg.serivce.AngelCandidateService;
import com.lg.serivce.AngelPicturesService;

public class AngelCandidateServiceImpl extends
		GenericServiceImpl<AngelCandidate> implements AngelCandidateService {
	
	private AngelPicturesService angelPicturesService;

	/**
	 * 查询个人档案和自我描述
	 */
	public AngelCandidate findAngelCandidate(String promoterId) {
		AngelCandidate c = null;
		try {
			c = this.getBaseDAO().getAngelCandidateDAO()
					.selectObjectByStatementId("findPersonInfo", promoterId);
			if (c != null) {
				int n = this.getBaseDAO().getAngelCandidateDAO()
						.countByStatementId("descsales", promoterId);
				c.setSales(n);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}

	/**
	 * 根据类型查询最天使榜
	 */
	public List findAngels(String type) {
		List list = null;
		try {
			list = this.getBaseDAO().getAngelCandidateDAO()
					.selectListByStatementId("findAngles", type, 0, 10);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 修改选手信息
	 */
	public void updateAngel(AngelCandidate angel){
		
		try{
			this.getBaseDAO().getAngelCandidateDAO().updateByPrimaryKeySelective(angel);
		}catch(Exception e){
			throw new OtherException(e.getMessage());
		}
	}
	
	@Override
	public List<AngelCandidate> findIndexUserVoteNum()
			throws BusinessException, OtherException {
		List<AngelCandidate> list = null;
		try {
			list = getBaseDAO().getAngelCandidateDAO().selectListByStatementId(
					"indexUserVoteNumber", null);
		} catch (Exception e) {
			throw new OtherException(e.getMessage());
		}
		return list;
	}

	@Override
	public boolean findCandidateIsExist(String nkname)
			throws BusinessException, OtherException {
		Example<Criteria> example = new Example<Criteria>(AngelCandidateCriteria.class);
		AngelCandidateCriteria angelCandidateCriteria = (AngelCandidateCriteria) example.createCriteria();
		angelCandidateCriteria.andNkNameEqualTo(nkname);
		angelCandidateCriteria.andDelFlagEqualTo(Del_Flag.No.getValue());
		AngelCandidate u = null;
		try {
			u = getBaseDAO().getAngelCandidateDAO().selectObjectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OtherException(e.getMessage());
		}
		if (u == null) {
			return false;
		}
		return true;
	}

	@Override
	public boolean addAngelCandidate(AngelCandidate angel,
			List<AngelPictures> list) throws BusinessException, OtherException {
		if(angel.getPromoterId() != null && !"".equals(angel.getPromoterId())){
			return false;
		}
		if(findCandidateIsExist(angel.getNkName())){
			return false;
		}
		angel.setRegDt(new Date());
		try {
			getBaseDAO().getAngelCandidateDAO().insertSelective(angel);
			angelPicturesService.addBatchePictures(list);
		} catch (Exception e) {
           return false;
		}
		return true;
	}

	public void setAngelPicturesService(AngelPicturesService angelPicturesService) {
		this.angelPicturesService = angelPicturesService;
	}
}
